package cn.ys.groupbuy;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by jh on 2015/6/17.
 */
public class CateView extends LinearLayout {
    private ArrayList<String> groups = new ArrayList<String>();
    private LinkedList<String> childrenItem = new LinkedList<String>();
    private SparseArray<LinkedList<String>> children = new SparseArray<LinkedList<String>>();
    private ListView groupList, childrenList;
    private Context mContext;
    private int imageResourceId[] = new int[]{R.mipmap.ic_category_food, R.mipmap.ic_category_new, R.mipmap.ic_category_movie, R.mipmap.ic_category_all, R.mipmap.ic_category_hotel, R.mipmap.ic_category_travel_around, R.mipmap.ic_category_entertainment, R.mipmap.ic_category_live, R.mipmap.ic_category_travel, R.mipmap.ic_category_shop, R.mipmap.ic_category_health, R.mipmap.ic_category_baby, R.mipmap.ic_category_other};
    private String groupsData[] = new String[]{"美食", "今日新单", "电影", "全部分类", "酒店", "周边游", "休闲娱乐", "生活服务", "旅游", "购物", "丽人", "亲子", "抽奖"};
    private String childrenData[][] = new String[][]{
            {"全部", "代金券", "蛋糕甜点", "火锅", "自助餐", "小吃快餐", "日韩料理", "西餐", "东北菜", "海鲜", "烧烤"},
            {"今日新单", "今日新单", "今日新单", "今日新单", "今日新单"},
            {"电影", "电影", "电影", "电影", "电影"},
            {"全部分类","全部分类","全部分类","全部分类","全部分类"},
            {"酒店","酒店","酒店","酒店","酒店"},
            {"全部","温泉","景点","主题公园","展览馆","农家乐","水上乐园","动植物园","海洋馆","游船","高空观景"},
            {"KTV","洗浴","演出赛事","桌游/电玩","运动健身","足疗按摩"},
            {"婚纱摄影","配镜","鲜花婚庆","汽车服务","亲子摄影","洗衣","体检保健"},
            {"境外游","景点门票","国内游"},
            {"女装","男装","内衣","女士鞋包","食品","家居日用","美妆","男士鞋包","运动户外","配饰手表"},
            {"美发","美甲","美容美体","瑜伽/舞蹈"},
            {"母婴护理","公园乐园","科普场馆","儿童培训","亲子写真","手工DIY","儿童话剧"},
            {"美梦成真","霸王餐","free游"}};
    private GroupAdapter groupAdapter;
    private ChildrenAdapter childrenAdapter;
    private View lastView=null;
    private View popupView;

    public CateView(Context context) {

        super(context);
        mContext = context;
        initListView();
    }

    public CateView(Context context, AttributeSet attrs) {

        super(context, attrs);
        mContext = context;
        initListView();
    }

    private void initListView() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.expand_view, this, true);
        groupList = (ListView) findViewById(R.id.listview);
        childrenList = (ListView) findViewById(R.id.listview2);

        for (int i = 0; i < groupsData.length; i++) {
            groups.add(i, groupsData[i]);
            LinkedList<String> tItem = new LinkedList<String>();
            for (int j = 0; j < childrenData[i].length; j++) {
                tItem.add(j, childrenData[i][j]);
            }
            children.put(i, tItem);
        }
        groupAdapter = new GroupAdapter();
        childrenAdapter = new ChildrenAdapter();

        groupList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               // System.out.println("the position value is : "+position+" the children size is : "+children.size());
                if (position < children.size()) {
                    Toast.makeText(mContext,"position : "+position,Toast.LENGTH_SHORT).show();
                    if(lastView!=null){
                        lastView.setBackgroundColor(0xffffffff);
                    }
                    childrenItem.clear();
                    childrenItem.addAll(children.get(position));
                    childrenAdapter.notifyDataSetChanged();
                    view.setBackgroundColor(0xfff4f4f4);
                    lastView=view;
                }
            }
        });
        groupList.setAdapter(groupAdapter);
        if (childrenItem.size() == 0) {
            childrenItem.addAll(children.get(0));
        }

        childrenList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        childrenList.setAdapter(childrenAdapter);
        groupList.setSelection(0);
        childrenList.setSelection(0);

    }

    class ChildrenAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return childrenItem.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View children_item = LayoutInflater.from(mContext).inflate(R.layout.children_item, null);
            TextView children_item_text1 = (TextView) children_item.findViewById(R.id.children_item_text1);
            TextView children_item_text2 = (TextView) children_item.findViewById(R.id.children_item_text2);
            children_item_text1.setText(childrenItem.get(position));
            children_item_text2.setText("10");
            return children_item;
        }
    }

    class GroupAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return groups.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View groups_item = LayoutInflater.from(mContext).inflate(R.layout.groups_item, null);
            ImageView groups_item_img = (ImageView) groups_item.findViewById(R.id.groups_item_img);
            TextView groups_item_text1 = (TextView) groups_item.findViewById(R.id.groups_item_text1);
            TextView groups_item_text2 = (TextView) groups_item.findViewById(R.id.groups_item_text2);
            groups_item_img.setImageResource(imageResourceId[position]);
            groups_item_text1.setText(groups.get(position));
            groups_item_text2.setText("100");
            return groups_item;
        }
    }

}
